#include "exportWlk.h"

ExportWlk::ExportWlk()
{

}

void ExportWlk::run(){

    DebugLog::write("导出wlk文件 exportWlk line10");

    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql;

#ifdef Q_OS_ANDROID
    QString remotePath = MainWindow::experimentSaveDataPath;
    QString localPath = SMBManager::I().SMB2Local(remotePath);

#else
    QString localPath = MainWindow::experimentSaveDataPath;
#endif






QString dataSql;

    sql = "Select * From FluorescenceData";
    query.exec(sql);
    while (query.next()) {
        dataSql.append(QString("Insert into FluorescenceData(SampleId,PositionNumber,CyclesNumber,FAM,VIC,ROX,CY5) Values ('%1','%2','%3','%4','%5','%6','%7') \n").arg(query.value("SampleId").toString(),query.value("PositionNumber").toString(),
                                                                                                                                                                       query.value("CyclesNumber").toString(),query.value("FAM").toString(),
                                                                                                                                                                       query.value("VIC").toString(),query.value("ROX").toString(),
                                                                                                                                                                       query.value("CY5").toString()));
    }
    QFile file(localPath);
    file.open(QFile::WriteOnly|QFile::Text|QIODevice::Append);

    QTextStream out(&file);
    QTextCodec* codec = QTextCodec::codecForName("GBK");
    out.setCodec(codec);
    QString addSql;
    sql="Select * from ExperimentInfo ";
    query.exec(sql);
    while (query.next()) {
        addSql=QString("INSERT or REPLACE into ExperimentInfo values('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10');").arg(query.value("ID").toString(),query.value("ExperimentName").toString(),
                                                                                                                     query.value("AmplGuid").toString(),query.value("Code").toString(),
                                                                                                                     query.value("System").toString(),query.value("RunTime").toString(),
                                                                                                                     query.value("Status").toString(),query.value("FinishTime").toString(),
                                                                                                                     query.value("ConsumableType").toString(),query.value("TestKit").toString());
        out<<addSql+"\n";
    }
    out<<dataSql;

    file.close();

#ifdef Q_OS_ANDROID
    DebugLog::write("exportWlk导出路径：remote" + remotePath + " local" + localPath);
    if(remotePath.contains("SMB://")){
        SMBManager::I().uploadFile(remotePath, localPath);
    }
#endif


    DebugLog::write("导出wlk文件 exportWlk end");
}
